var ClubBlendQuickJoinWindow = Window.extend({
    _ccsFileName: res.ClubBlendQuickJoinWindow_json,
    _clubId: null,
    _selectFloor: -1,
    _selectPlayerNum: -1,
    initWindow: function (clubId) {
        this._clubId = clubId;
        this.hideView();
        this._initClubWafaInfo();

        for (var i = 0; i < this._wanfaList.length; i++) {
            var wanfaInfo = this._wanfaList[i];
            this._nodeList['wanfaNode' + i].setVisible(true);
            this._nodeList['wanfaNode' + i + '.wanfaName'].setString(wanfaInfo.name);
            this._nodeList['wanfaNode' + i + '.wanfaType'].setString(wanfaInfo.blendTips);
            this._nodeList['wanfaNode' + i + '.wanfaRound'].setString(wanfaInfo.jushu);
        }
        this._clickFloor({index: 0});
    },

    _initClickListeners: function () {
        this._clickListeners = {
            'btn_ok': '_clickOk'
        };
        var i;
        for (i = 2; i <= 9; i++) {
            if (this._nodeList['checkBox' + i]) {
                this._nodeList['checkBox' + i].index = i;
                this._clickListeners['checkBox' + i] = '_clickCheck';
            }
        }
        for (i = 0; i < 4; i++) {
            this._nodeList['wanfaNode' + i].index = i;
            this._clickListeners['wanfaNode' + i] = '_clickFloor';
        }
    },

    hideView: function () {
        var i;
        for (i = 2; i <= 4; i++) {
            this._nodeList['checkBox' + i].setVisible(false);
        }
        for (i = 0; i < 4; i++) {
            this._nodeList['wanfaNode' + i].setVisible(false);
        }
    },

    _clickOk: function () {
        clubMsgManager.requestQuickJoinRoom(this._clubId, this._selectFloor, this._selectPlayerNum);
        this.close();
    },

    _clickCheck: function (target) {
        var index = target.index;
        var unselect_color = cc.color("#7a5535");
        var select_color = cc.color("#15b62c");
        for (var i = 2; i <= 9; i++) {
            if (this._nodeList['checkBox' + i]) {
                this._nodeList['checkBox' + i + '.select'].setVisible(index == i);
                this._nodeList['checkBox' + i + '.label'].setVisible((index == i) ? select_color : unselect_color);
            }
        }
        this._selectPlayerNum = index;
    },

    _clickFloor: function (target) {
        var index = target.index;
        //图片楼层  和 数据上对应不上
        var viewFloor = [0, 4, 3, 2, 1];
        for (var i = 0; i < this._wanfaList.length; i++) {
            var wanfaInfo = this._wanfaList[i];
            var floor = viewFloor[wanfaInfo.floor];
            if (index == i) {
                this._selectFloor = wanfaInfo.floor;
                this._nodeList['wanfaNode' + i].setTexture(res['quickSelectBack']);
                this._nodeList['wanfaNode' + i + '.floor'].setTexture(res['quickFloor' + floor]);
            } else {
                this._nodeList['wanfaNode' + i].setTexture(res['quickSelectUnBack']);
                this._nodeList['wanfaNode' + i + '.floor'].setTexture(res['quickFloorUn' + floor]);
            }
        }

        var playerMap = this._wanfaList[index].playerMap;
        var keys = _.keys(playerMap);
        var viewChecks = [];
        //根据当前选择的玩法人数 刷新人数复选框显隐
        for (i = 2; i <= 9; i++) {
            var check = this._nodeList['checkBox' + i];
            if (check == undefined || !cc.sys.isObjectValid(check)) {
                continue;
            }
            if (keys.indexOf(i + '') >= 0) {
                check.setVisible(true);
                viewChecks.push(i);
            } else {
                check.setVisible(false);
            }
        }
        //根据当前显示的复选框 重置坐标
        for (var i = 0; i < viewChecks.length; i++) {
            this._nodeList['checkBox' + viewChecks[i]].x = -217 + i * 180;
            this._nodeList['checkBox' + viewChecks[i] + '.select'].setVisible(i == 0);
        }
        this._clickCheck({index: viewChecks[0]});
    },

    /**
     * 初始化亲友圈玩法信息
     * @private
     */
    _initClubWafaInfo: function () {
        this._wanfaList = [];
        var wanfas = ClubDataManager.getClubWanfas(this._clubId);
        for (var i = 0; i < wanfas.length; i++) {
            var wanfa = {};
            wanfa.name = ClubDataManager.getDesc2WanfaName(wanfas[i]);
            wanfa.floor = wanfas[i].pos;
            wanfa.blend = false;
            wanfa.blendTips = '混桌';
            wanfa.renshu = 0;

            var option = JSON.parse(wanfas[i].options);
            var playerMap = ClubDataManager.analysisWanFaInfo(option)['playerMap'];
            var keys = _.keys(playerMap);
            if (keys.length > 1) {
                wanfa.blend = true;
            } else if (keys.length == 1) {
                wanfa.renshu = keys[0];
                wanfa.blendTips = keys[0] + '人';
            }
            wanfa.playerMap = playerMap;
            wanfa.jushu = option.jushu + '局';
            this._wanfaList.push(wanfa);
        }
    },
});